package com.gxa.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.admin.mapper.GoodsMapper;
import com.gxa.admin.pojo.dto.GoodsDto;
import com.gxa.admin.pojo.vo.GoodsVo;
import com.gxa.admin.service.GoodsService;
import com.gxa.admin.utils.HttpClientUtil;
import com.gxa.admin.utils.QueryResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author：lizhijun
 * @Package：{PROJECT_NAME}
 * @name：{DATE} {NAME}
 */
@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public QueryResult getGoodsList(GoodsDto goodsDto) {
        goodsDto.setPage(1);
        goodsDto.setLimit(10);
        Page<GoodsVo> page= PageHelper.startPage(goodsDto.getPage(),goodsDto.getLimit());
        List<GoodsVo> goodsList = goodsMapper.QueryGoods(goodsDto);
        QueryResult result = new QueryResult();
        result.setCount(page.getTotal());
        result.setData(goodsList);
        //TODO 查询商品列表
        return result;
    }

    @Override
    public int addGoodsList(GoodsDto goodsDto) {
        int i = this.goodsMapper.AddGoods(goodsDto);
        this.goodsMapper.AddImgUrl(goodsDto);

            return i;

    }

    @Override
    public void updateGoods(GoodsDto goodsDto) {
        this.goodsMapper.UpdateGoods(goodsDto);
        //调用mobile接口删除redis中缓存
        HttpClientUtil.doGet("http://localhost:8081/Goods/deleteGoodsCache?goodsId="+goodsDto.getGoodsId());
    }

    @Override
    public int deleteGoods(GoodsDto goodsDto) {
        int result = this.goodsMapper.DeleteGoods(goodsDto);
        //调用mobile接口删除redis中缓存
        HttpClientUtil.doGet("http://localhost:8081/Goods/deleteGoodsCache?goodsId="+goodsDto.getGoodsId());
        return result;
    }
}
